package com.itheima.putige.putige.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.putige.putige.common.BaseContext;
import com.itheima.putige.putige.common.R;
import com.itheima.putige.putige.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "loginfilter",urlPatterns = "/*")
@Slf4j
public class LoginFilter implements Filter {
    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String[] url = new String[]{
                "/employee/login",
                "/employee/loginout",
                "/employee/repwd",
                "/backend/**",
                "/front/**",
                "/js/**",
                "/api/**",
                "/plugins/**",
                "/images/**",
                "/",
                "/employee/findpwd/**",
                "/employee/findpwd",
                "/user/sendMsg",
                "/user/login",
                "/order/echarts",//todo 测试开放
                "/favicon.ico",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"
        };
        String uri = request.getRequestURI();
        log.info("拦截到请求:" + uri);
        if (check(url,uri)){
            log.info("放行:" + uri);
            filterChain.doFilter(request,response);
            return;
        }
        Employee employee = (Employee) request.getSession().getAttribute("employee");
        if (employee != null){
            Long empId = employee.getId();
            BaseContext.setCurrentId(empId);
            log.info("放行:" + uri);
            filterChain.doFilter(request,response);
            return;
        }
        if (request.getSession().getAttribute("user") != null){
            log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("user"));
            Long userId = (Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);
            filterChain.doFilter(request,response);
            return;
        }
        log.info("用户未登录");
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }

    /**
     * 路径匹配,判断是否需要拦截
     * @param urls 放行地址数组
     * @param requestUri 判断地址
     * @return
     */
    public boolean check(String[] urls,String requestUri){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestUri);
            if (match)return true;
        }
        return false;
    }
}
